package com.project.psc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.psc.bean.BiddBean;
import com.project.psc.mapper.IBiddMapper;
import com.project.psc.service.IBiddService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class BiddServiceImpl implements IBiddService {
    @Autowired
    private IBiddMapper biddMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void add(BiddBean biddBean,Long supplierId) {
        biddBean.setSupplierId(supplierId);
        String biddId="B"+System.currentTimeMillis();
        biddBean.setBiddId(biddId);

        String json= JSON.toJSONString(biddBean);

        biddMapper.insert(biddBean);
        rabbitTemplate.convertAndSend("supplierOne","supplier.sendBiddTable",json);
    }

    @Override
    public IPage<BiddBean> cutByItem(Integer pageNO,Long supplierId, String title, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<BiddBean> qw=new QueryWrapper<>();
        qw.eq("fk_supplierId",supplierId);
        qw.orderByDesc("b_date");
        if (title!=null&&title.length()!=0){
            qw.like("b_title",title);
        }
        if (endDate!=null){
            qw.le("b_date",endDate);
        }
        if (startDate!=null){
            qw.ge("b_date",startDate);
        }

        return biddMapper.selectPage(new Page<>(pageNO,6),qw);
    }

    @Override
    public BiddBean findById(Long id) {
        return biddMapper.selectById(id);
    }


}
